package edu.purdue.cs.aggr.accumulators;

import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.LinkedList;
import java.util.List;

import edu.purdue.cs.aggr.AbstractAccumulator;
import edu.purdue.cs.aggr.Accumulator;

public class GrepAccumulator extends AbstractAccumulator implements Accumulator<String, LinkedList<String>> {

	public GrepAccumulator(String sourceId)
  {
    super(sourceId);
  }

  private LinkedList<String> lines = new LinkedList<String>();
	public final String PATTERN = "Time";
	

  @Override
	public void writeExternal(ObjectOutput out) throws IOException {
    super.writeExternal(out);
		out.writeObject(lines);
	}

	@Override
	public void readExternal(ObjectInput in) throws IOException,
			ClassNotFoundException {
	  super.readExternal(in);
		lines = (LinkedList<String>) in.readObject();
	}

	@Override
	public void initialize(String input) {
		// TODO Auto-generated method stub
	}

	@Override
	public void addValue(String input) {
		if(input.contains(PATTERN)) {
			lines.add(input);
		}
	}

	@Override
	public LinkedList<String> getAccumulation() {
		return lines;
	}

	@Override
	public void addAccumulators(
			List<Accumulator<String, LinkedList<String>>> accumulators) {
		for (Accumulator<String, LinkedList<String>> acc : accumulators)
		lines.addAll(acc.getAccumulation());
	}
}
